package com.graduation.cdm.funactivity.util;

import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.graduation.cdm.funactivity.MyApplication;
import com.graduation.cdm.funactivity.bean.ActivityBean;
import com.graduation.cdm.funactivity.bean.User;
import com.graduation.cdm.funactivity.bean.UserBean;

import java.util.HashMap;
import java.util.Map;

import static com.graduation.cdm.funactivity.util.GlobalConstant.preUrl;
import static com.graduation.cdm.funactivity.util.xUtilsImageUtils.displayHead;

/**
 * Created by cdm on 2017/3/22.
 */

public class getUtils {

    public static void getUser(final ActivityBean.Activity activity, final ImageView head, final TextView name, final TextView phone){
        String url = preUrl + "user/getUser";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                System.out.println("=user="+s.trim());
                Gson gson = new Gson();
                UserBean userBean = gson.fromJson(s,UserBean.class);
                User user = userBean.userList.get(0);
                if (user!=null) {
                    name.setText(user.getUserName());
                    phone.setText(user.getUserPhone());
                    displayHead(head,preUrl+user.getImageUrl());
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                System.out.println("==="+volleyError.toString());
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map=new HashMap<>();
                map.put("id",activity.userId+"");
                return map;
            }
        };
        MyApplication.getRequestQueue().add(stringRequest);
    }

    public static void getUser(final int userId, final ImageView head, final TextView name){
        String url = preUrl + "user/getUser";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                System.out.println("=userId="+s.trim());
                Gson gson = new Gson();
                UserBean userBean = gson.fromJson(s,UserBean.class);
                User user = userBean.userList.get(0);
                if (user!=null) {
                    name.setText(user.getUserName());
                    displayHead(head,preUrl+user.getImageUrl());
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                System.out.println("==="+volleyError.toString());
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map=new HashMap<>();
                map.put("id",userId+"");
                return map;
            }
        };
        MyApplication.getRequestQueue().add(stringRequest);
    }

}
